/*
 *  Copyright 2008 Danny Lagrouw
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package org.bastion.query;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.bastion.adapter.QueryAdapter;
import org.bastion.service.QueryService;

/**
 * Encapsulates a query for domain objects from a persistent store outside the
 * domain. Queries can be specified using either a Predicate/Specification
 * pattern, or a query name. Some persistency services will be able to query by
 * Predicate; others will query using a named query; still others will support
 * both. Therefore, ideally a query will be specified using both methods. The
 * {@link QueryAdapter} will ask the {@link QueryService} which method is
 * supported or preferred.
 * 
 * @author Danny Lagrouw
 * 
 * @param <T>
 */
public class DomainQuery<T extends Object> implements IDomainQuery<T> {

	private final Class<T> domainClass;

	private Matcher<T> matcher;

	private String name = null;

	private Map<String, Object> parameters = new HashMap<String, Object>();

	public DomainQuery(Class<T> domainClass) {
		this.domainClass = domainClass;
	}

	/* (non-Javadoc)
	 * @see org.bastion.query.IDomainQuery#getDomainClass()
	 */
	public Class<T> getDomainClass() {
		return domainClass;
	}

	/**
	 * Specifies a predicate for testing if a queried object should be included
	 * in the query result.
	 * 
	 * @param matcher
	 *            the predicate defining the query;
	 *            {@link Matcher#match(Object)} will return true for objects to
	 *            be included in the query result.
	 */
	public void setMatcher(Matcher<T> matcher) {
		this.matcher = matcher;
	}

	/**
	 * Specifies the name of a named query known to the persistency service.
	 * 
	 * @param name
	 *            the query name.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Returns the matcher for this query. If no specific matcher
	 * has been set, an {@link AlwaysMatcher} will be returned.
	 * 
	 * @return the matcher for this query.
	 * @see org.bastion.query.IDomainQuery#getMatcher()
	 */
	public Matcher<T> getMatcher() {
		return matcher == null ? new AlwaysMatcher<T>() : matcher;
	}

	/* (non-Javadoc)
	 * @see org.bastion.query.IDomainQuery#getName()
	 */
	public String getName() {
		return name;
	}

	/**
	 * Specifies a named parameter for use with a named query.
	 * 
	 * @param parameter
	 *            the parameter name (not null).
	 * @param value
	 *            the parameter value.
	 */
	public void setParameter(String parameter, Object value) {
		parameters.put(parameter, value);
	}

	/* (non-Javadoc)
	 * @see org.bastion.query.IDomainQuery#getParameters()
	 */
	public Map<String, Object> getParameters() {
		return Collections.unmodifiableMap(parameters);
	}

}
